﻿$(function() {
    $(document).ajaxStop($.unblockUI);
});

function supplyOperate(obj, srId) {
    $.blockUI({ message: '<h3>数据提交中...</h3>' });
    var supllyStatus = $(obj).text() == '取消供应' ? 1 : 0;
    $.ajax({
        url: 'product.ashx?Param=sellercancelsupply',
        data: 'w=a&srId=' + srId + '&supllyStatus=' + supllyStatus,
        dataType: 'json',
        cache: false,
        async: true,
        success: function(jsonResult) {
            if (jsonResult['code'] == "1") {
                if (supllyStatus == 1) {
                    $(obj).text('继续供应').css('color', '#f00');
                    $(obj).parent().prev().text('取消中');
                } else {
                    $(obj).text('取消供应').css('color', '#54862c');
                    $(obj).parent().prev().text('供应中');
                }

                if (jsonResult['priceArray']) {
                    var pr = jsonResult['priceArray'];
                    $('.product_' + jsonResult['productId']).each(function(entryIndex, entry) {
                        var cur = $('input:eq(0)', entry);
                        if ($.trim($(cur).attr('bn')) == pr[entryIndex]['businessNature']) {
                            $('span:eq(0)', entry).text(pr[entryIndex]['sellerPrice']);
                        }
                    });
                }
            }
        }
    });
}

function clientSubmitPrice(obj, productId) {
    var isNext = false;
    var pattern = /^[0-9]+([.]\d{1,2})?$/;
    var priceArray = [];
    var countCanceling = 0;

    $('.product_' + productId).each(function(entryIndex, entry) {
        var inp = $(entry).find('input')[0];
        var lbl = $(entry).find('label')[0];

        if ($(inp).val().length != 0) {
            if (pattern.test($(inp).val())) {
                if ($.trim($(inp).parent().prev().prev().text()) != '取消中') {
                    priceArray.push({ 'productId': productId, 'price': $(inp).val(), 'businessNature': $(inp).attr('bn'), 'unit': encodeURIComponent($(lbl).text()) });
                } else {
                    countCanceling++;
                }

                isNext = true;
            } else {
                isNext = false;
                alert("请输入正确的报价!");
                inp.select();
                inp.focus();
                return false;
            }
        } else {
            isNext = true;
        }
    });

    if (!isNext) {
        return false;
    }

    if (priceArray.length != 0 && countCanceling >= 0) {
        $.blockUI({ message: '<h3>数据提交中...</h3>' });
        $.ajax({
            url: 'product.ashx?Param=sellersubmitprice',
            data: 'w=a&companyId=' + $('#ddlCompany').val() + '&contactMan=&reason=&tArray=' + Object.serialize2Str(priceArray),
            dataType: 'json',
            cache: false,
            async: true,
            success: function(jsonResult) {
                if (jsonResult['code'] == 1) {
                    alert('已超过修改的价格的时间!');
                } else if (jsonResult['code'] == 2) {
                    alert('提交失败！');
                }
                else if (jsonResult['code'] == 4) {
                    alert('当前商品处于"不供应"状态，不能报价；请将商品设置为可"供应"状态，然后再报价!');
                }
                else if (jsonResult['code'] == 3) {
                    if (jsonResult['priceArray']) {
                        var pr = jsonResult['priceArray'];
                        $('.product_' + jsonResult['productId']).each(function(entryIndex, entry) {
                            var cur = $('input:eq(0)', entry);
                            if ($.trim($(cur).attr('bn')) == pr[entryIndex]['businessNature']) {
                                $('span:eq(0)', entry).text(pr[entryIndex]['sellerPrice']);
                            }
                        });
                        alert('提交成功！');
                    }
                }
            }
        });
    } else {
        if (countCanceling > 0) {
            alert('当前商品处于"不供应"状态，不能报价；请将商品设置为可"供应"状态，然后再报价!');
            return false;
        } else {
            alert("请至少输入一种价格！");
            return false;
        }
    }
}